package com.project.hqshorttaxi.controller;

import com.github.pagehelper.PageHelper;
import com.project.hqshorttaxi.aop.TagLog;
import com.project.hqshorttaxi.dto.MenuParam;
import com.project.hqshorttaxi.entity.Menu;
import com.project.hqshorttaxi.entity.RoleMenu;
import com.project.hqshorttaxi.service.MenuService;
import com.project.hqshorttaxi.service.RoleMenuService;
import com.project.hqshorttaxi.util.PageInfo;
import com.project.hqshorttaxi.util.RestResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/menu/")
public class MenuController {

    @Resource
    MenuService menuService;
    @Resource
    RoleMenuService menuRoleService;

    /**
     * 新增菜单
     */
    @PostMapping("add")
    @TagLog(operation = 2, content = "新增菜单")
    public RestResult add(@RequestBody Menu param) {
        try {
            List<Menu> list = menuService.listAll();
            param.setMenuId(list.size() + 1);
            menuService.insert(param);
            return RestResult.ok();
        } catch (Exception e) {
            return RestResult.error(e.getMessage());
        }
    }

    /**
     * 更新菜单
     */
    @PostMapping("update")
    @TagLog(operation = 3, content = "更新菜单")
    public RestResult update(@RequestBody Menu param) {
        try {
            Menu entity = menuService.getById(param.getId());
            if (entity == null) {
                return RestResult.error("菜单不存在");
            } else {
                param.setUpdateTime(new Date());
                menuService.update(param);
                return RestResult.ok();
            }
        } catch (Exception e) {
            return RestResult.error(e.getMessage());
        }
    }

    /**
     * 删除菜单
     */
    @PostMapping("delete/{id}")
    @TagLog(operation = 4, content = "删除菜单")
    public RestResult userDelete(@PathVariable Integer id) {
        try {
            Menu entity = menuService.getById(id);
            if (entity == null) {
                return RestResult.error("菜单不存在");
            } else {
                List<RoleMenu> menuRoleList = menuRoleService.getByMenuId(id);
                if (menuRoleList.size() > 0) {
                    return RestResult.error("该菜单已存在使用权限");
                }
                if (entity.getPid() == 0) {
                    List<Menu> menuSubList = menuService.listByPid(entity.getPid());
                    if (menuSubList.size() > 0) {
                        return RestResult.error("该菜单存在子菜单，请先删除子菜单");
                    }
                }
                entity.setDeleteFlag(1);
                entity.setUpdateTime(new Date());
                menuService.update(entity);
                return RestResult.ok();
            }
        } catch (Exception e) {
            return RestResult.error(e.getMessage());
        }
    }

    /**
     * 菜单列表
     */
    @PostMapping("page")
    @TagLog(operation = 0, content = "查询菜单列表")
    public RestResult page(@RequestBody MenuParam param) {
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        return RestResult.ok(new PageInfo<>(menuService.list(param)));
    }

    /**
     * 根据ID获取菜单
     */
    @GetMapping("{id}")
    @TagLog(operation = 0, content = "根据ID获取菜单详细信息")
    public RestResult pageById(@PathVariable Integer id) {
        return RestResult.ok(menuService.getById(id));
    }
}
